/**
 * Created by li on 2017/2/20.
 */
//game start cartoon
var GameCartoonLayer = cc.Layer.extend({

    ctor:function () {
        this._super();


        //var frame = new cc.SpriteFrame(res.walkRight_small_png,cc.rect(14*i,0,14,16));
        //spriteFrame.push(frame);
        //var animation = new cc.Animation(spriteFrame,delay);
        //var anim = new cc.Animate(animation);
        //cc.animationCache.addAnimation(anim,type);
        //var anim = cc.animationCache.getAnimation(animId);
        //var action = cc.repeatForever(anim);
        //this.mySprite.runAction(action);

        this.bgSprite = new cc.Sprite(res.cartoon_png,cc.rect(1,1,200,200));
        var scaleX = size.width/this.bgSprite.width;
        var scaleY = size.height/this.bgSprite.height;
        this.bgSprite.attr({
            anchorX:0,
            anchorY:0,
            scaleX:scaleX,
            scaleY:scaleY,
        })
        this.addChild(this.bgSprite);

        cc.audioEngine.playMusic(res.cartoonMusic_mp3,true);

        var spriteFrame = [];
        var delay=0.25;
        for(var i=0;i<15;i++){
            for(var j= 0;j<20;j++){
                var frame = new cc.SpriteFrame(res.cartoon_png,cc.rect(1+201*j,1+201*i,200,200));
                spriteFrame.push(frame);
            }
        }

        var animation = new cc.Animation(spriteFrame,delay);
        var anim = new cc.Animate(animation);
        //cc.animationCache.addAnimation(anim,100);
        this.action = cc.repeat(anim,1);
        //console.log(action.isDone())
        this.bgSprite.runAction(this.action);

        var mouseEvent = cc.eventManager.addListener({
            event: cc.EventListener.MOUSE,
            //onMouseMove: function(event){
            //    var str = "MousePosition X: " + event.getLocationX() + "  Y:" + event.getLocationY();
            //},
            onMouseDown: function(event){
                //var str = "Mouse Down detected, Key: " + event.getButton();
                cc.eventManager.removeListener(mouseEvent)
                var transition=new cc.TransitionCrossFade(1,new GameMenuScene());
                cc.director.runScene(transition);
            },
        },this);

        this.schedule(this.MyUpdate,2,cc.REPEAT_FOREVER);

    },
    MyUpdate:function(dt){
        //check cartoon is over,skip next scene,  now is not good
        if (this.action.isDone()){
            var transition=new cc.TransitionCrossFade(1,new GameMenuScene());
            cc.director.runScene(transition);
        }
    },

});


var GameCartoonScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new GameCartoonLayer();
        this.addChild(layer);
    }
});